package com.quan.commons.context;

import cn.hutool.core.util.BooleanUtil;
import io.netty.util.concurrent.FastThreadLocal;

/**
 * 保存当前请求traceId的线程本地对象容器组
 */
public class TraceIdHolder {

    /**
     * 启用开关容器
     */
    private static final FastThreadLocal<Boolean> SWITCH_FLAG = new FastThreadLocal<>();
    private static final FastThreadLocal<String> TRACE_ID_CONTEXT = new FastThreadLocal<>();
    private static final FastThreadLocal<String> LOG_TIMES_PREFIX_CONTEXT = new FastThreadLocal<>();
    private static final FastThreadLocal<Long> LOG_TIMES_CONTEXT = new FastThreadLocal<>();

    /**
     * 容器组初始化
     */
    public static void init() {
        SWITCH_FLAG.set(true);
    }

    /**
     * 开关开启的情况下，将traceId放入容器中
     *
     * @param traceId 链路ID
     */
    public static void resetByTraceId(String traceId) {
        Boolean switchFlag = SWITCH_FLAG.get();
        if (BooleanUtil.isTrue(switchFlag)) {
            TRACE_ID_CONTEXT.set(traceId);
            LOG_TIMES_PREFIX_CONTEXT.set("");
            LOG_TIMES_CONTEXT.set(0L);
        }
    }

    /**
     * 开关开启的情况下，将日志序号的前缀放入容器中
     *
     * @param prefix 序号前缀
     */
    public static void setLogTimesPrefix(String prefix) {
        Boolean switchFlag = SWITCH_FLAG.get();
        if (BooleanUtil.isTrue(switchFlag)) {
            LOG_TIMES_PREFIX_CONTEXT.set(prefix);
        }
    }

    /**
     * 开关开启的情况下，将最新的日志打印次数放入容器中
     *
     * @param logTimes 打印次数
     */
    public static void setLogTimes(Long logTimes) {
        Boolean switchFlag = SWITCH_FLAG.get();
        if (BooleanUtil.isTrue(switchFlag)) {
            LOG_TIMES_CONTEXT.set(logTimes);
        }
    }

    /**
     * 开关开启的情况下，尝试返回容器中的traceId
     * 开关关闭情况下返回null
     *
     * @return java.lang.String
     */
    public static String get() {
        Boolean switchFlag = SWITCH_FLAG.get();
        if (BooleanUtil.isTrue(switchFlag)) {
            return TRACE_ID_CONTEXT.get();
        } else {
            return null;
        }

    }

    /**
     * 获取日志序号的前缀
     *
     * @return java.lang.String
     */
    public static String getLogTimesPrefix() {
        Boolean switchFlag = SWITCH_FLAG.get();
        if (BooleanUtil.isTrue(switchFlag)) {
            return LOG_TIMES_PREFIX_CONTEXT.get();
        } else {
            return null;
        }
    }

    /**
     * 获取当前次数
     *
     * @return java.lang.Long
     */
    public static Long getLogTimes() {
        Boolean switchFlag = SWITCH_FLAG.get();
        if (BooleanUtil.isTrue(switchFlag)) {
            return LOG_TIMES_CONTEXT.get();
        } else {
            return null;
        }
    }

    /**
     * 获取当前次数+1后的计算结果
     *
     * @return java.lang.Long
     */
    public static Long increaseAndGetLogTimes() {
        Boolean switchFlag = SWITCH_FLAG.get();
        if (BooleanUtil.isTrue(switchFlag)) {
            Long oldValue = LOG_TIMES_CONTEXT.get();
            Long newValue = oldValue + 1;
            LOG_TIMES_CONTEXT.set(newValue);
            return newValue;
        } else {
            return null;
        }
    }

    /**
     * 删除保存的用户
     */
    public static void remove() {
        SWITCH_FLAG.remove();
        TRACE_ID_CONTEXT.remove();
    }
}
